package com.example.myshop.slice;

import com.example.myshop.ResourceTable;
import com.example.myshop.base.BaseFraction;
import com.example.myshop.cart.CartFraction;
import com.example.myshop.category.CategoryFraction;
import com.example.myshop.home.HomeFraction;
import com.example.myshop.recommend.RecommendFraction;
import com.example.myshop.setting.SettingFraction;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.ability.fraction.FractionManager;
import ohos.aafwk.ability.fraction.FractionScheduler;
import ohos.aafwk.content.Intent;
import ohos.agp.components.RadioContainer;

import java.util.ArrayList;

public class MainAbilitySlice extends AbilitySlice {
    private RadioContainer radioContainer;  //单选框组
    private FractionManager fm;        //Fraction管理器
    private BaseFraction tempFraction;  //当前展示页面Fraction
    private ArrayList<BaseFraction> fractions; //Fraction列表

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        radioContainer = findComponentById(ResourceTable.Id_radio_container_1);//初始化单选框
        initFraction();//初始化Fraction和Fraction管理器
        radioContainer.mark(0);//将单选框的选择设置为第0为
        tempFraction = fractions.get(0);//将初始加载的页面设置为第0个Fraction
        addFraction();//通过FractionScheduler 在stack_layout中添加第0个Fraction

        initListener();//初始化点击事件
    }
    public void addFraction() {
        FractionScheduler scheduler = fm.startFractionScheduler();
        scheduler.add(ResourceTable.Id_stack_layout_1, fractions.get(0)).submit();
    }

    public BaseFraction getFraction(int index) {//通过下表获取Fraction对象
        if (fractions != null && fractions.size() > 0) {
            BaseFraction fraction = fractions.get(index);
            return fraction;
        }
        return null;
    }
    public void switchFraction(BaseFraction fromFraction, BaseFraction toFraction) {
        if (fromFraction != toFraction) {//判断是否替换的为同一个页面
            tempFraction = toFraction;
            FractionScheduler fs = fm.startFractionScheduler();
//将其替换为新的fraction
            fs.replace(ResourceTable.Id_stack_layout_1, toFraction).submit();
        }
    }

    public void initListener() {
        radioContainer.setMarkChangedListener((radioContainer, index) -> {
            BaseFraction fraction = getFraction(index);
//绑定点击事件，如果点击则调用switchFraction方法替换页面
            switchFraction(tempFraction, fraction);
        });
    }


    public void initFraction() {
        try{
            fm = ((FractionAbility) getAbility()).getFractionManager();//初始化Fraction管理器


            fractions = new ArrayList<>();//初始化Fraction，将创建好的五个Fraction添加进列表中
            fractions.add(new HomeFraction());
            fractions.add(new CategoryFraction());
            fractions.add(new RecommendFraction());
            fractions.add(new CartFraction());
            fractions.add(new SettingFraction());
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
    }



    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
